package servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import database.DBHandler;
import forum.Member;

/**
 * Servlet implementation class AddFriendServlet
 */
public class EditFriendServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditFriendServlet() {
        super();
    }
    
    @Override
    public void init(){
    	try {
			super.init();
		} catch (ServletException e) {
			System.err.println("Error in initializing register servlet!!!");
			e.printStackTrace();
		}
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();		
		//DatabaseInterface db = new DatabaseStub();		
		String action = request.getParameter("do");		
		String friendName = request.getParameter("name");				
		if(action.equals("add")){			
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/jsp/editFriend.jsp");						
			dispatcher.forward(request, response);			
		}
		else 
			if(action.equals("delete")){			
				//TODO update database handler
				if (DBHandler.getInstance().removeFriend(session, friendName) == 1){
					List<Member> friends = DBHandler.getInstance().getFriends(session);//(ArrayList<String>) db.removeFriend(session, friendName);	
					List<String> friendsNames = new ArrayList<String>();
					for (int i = 0; i < friends.size(); i++){
						friendsNames.add(friends.get(i).getUsername());
					}
					request.setAttribute("friends", friendsNames);
					RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/jsp/friendsList.jsp");						
					dispatcher.forward(request, response);
				}
			}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();		
		//DatabaseInterface db = new DatabaseStub();			
		String friendName = request.getParameter("name");	
		if (DBHandler.getInstance().addFriend(session, friendName) == 0){
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/jsp/Error.jsp");
			request.setAttribute("errorMsg", "Error in adding friend. \nMaybe this friend is already exists in your friends list or not exist at all");
			request.setAttribute("errorLink", "http://" + request.getServerName() + ":"
					+ request.getServerPort() + request.getContextPath()+"/viewFriends");
			dispatcher.forward(request, response);
		}
		else {
			response.sendRedirect(request.getContextPath() + "/viewFriends");
		}
	}
	
	@Override
	public void destroy(){
		super.destroy();
	}

}
